<template>
  <div>
    <el-form :model="formData" ref="form" :rules="rules">
      <el-form-item label="本单位历史审核意见">
        <el-table :data="formData.list" border stripe height="300" v-loading="loading">
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="name" label="审核单位">
          </el-table-column>
          <el-table-column prop="message" label="审核信息">
          </el-table-column>
          <el-table-column prop="create_date" label="审核时间" width="100">
            <template slot-scope="{row}">
              {{$util.formatDate(row.create_date)}}
            </template>
          </el-table-column>
          <el-table-column label="审核状态" width="80">
            <template slot-scope="{row}">
              {{row.check_status | f_checkStatus}}
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item label="本单位审核意见">
        <el-input v-model="formData.message" size="normal" type="textarea" :rows="4" clearable></el-input>
      </el-form-item>
    </el-form>
    <div class="center">
      <el-button type="primary" size="default" @click="cancel">取消</el-button>
      <el-button type="danger" size="default" @click="quitShow = true" :loading="saveLoading">驳回</el-button>
      <el-button type="success" size="default" @click="submit" :loading="saveLoading">通过审核</el-button>
    </div>
    <!-- 驳回信息 -->
    <el-dialog title="审核信息" :visible.sync="quitShow" width="30%" @close="quitShow = false" append-to-body
      :destroy-on-close="true">
      <el-radio-group v-model="quitStatus">
        <el-radio v-for="item in quitDept" :key="item.value" :label="item.value" border style="margin-right:5px">
          {{item.label}}
        </el-radio>
      </el-radio-group>
      <span slot="footer">
        <el-button type="danger" size="default" @click="quitShow = false">关闭</el-button>
        <el-button type="primary" size="default" @click="reject">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { f_checkStatus } from '@/utils/filters.js'
import { mapGetters } from 'vuex'
export default {
  name: 'Verify',
  components: {

  },
  props: {
    verifyParams: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      loading: false,
      saveLoading: false,
      formData: {},
      rules: {},
      quitShow: false,
      quitStatus: '',
      quitDept: [],
    }
  },
  created () {

  },
  mounted () {
    if (this.verifyParams.id) {
      this.init()
    }
  },
  methods: {
    async init () {
      this.loading = true
      // 取出审核日志
      const res = await this.$http('verifyLog', {
        id: this.verifyParams.id,
        status: this.verifyParams.status,
      })
      if (res.govern_check_msg) {
        res.message = res.govern_check_msg
      } else { // 未审核过的部门
        res.message = ''
      }
      this.formData = res
      // 判断驳回机构
      const checkLog = await this.$http('verifyPower', {
        apply_batch: this.verifyParams.apply_batch,
        id: this.verifyParams.id
      })
      for (let item of checkLog) {
        if (item.pow_name === '内设部门审核权限') {
          this.quitDept = [
            { value: '0', label: '填报单位' }
          ]
        } else if (item.pow_name === '保障部门审核权限') {
          this.quitDept = [
            { value: '0', label: '填报单位' },
            { value: '3', label: '内设部门' }
          ]
        } else if (item.pow_name === '电子政务部审核权限') {
          this.quitDept = [
            { value: '0', label: '填报单位' },
            { value: '3', label: '内设部门' }
          ]
        } else {
          this.quitDept = [
            { value: '0', label: '填报单位' },
            { value: '2', label: '保障部门' },
            { value: '3', label: '内设部门' }
          ]
        }
      }
      this.loading = false
    },
    cancel () {
      this.saveLoading = false
      this.$emit('cancel');
    },
    reject () {
      this.$confirm('是否确认驳回?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.reject1()
      }).catch(() => {

      });
    },
    async reject1 () {
      // 判断是内设部门还是保障部门下的项目
      let status = this.quitStatus
      let param = {}
      // 审核时间
      let date = new Date()
      let today = this.$util.formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      let check_end_time = await this.$http('checkDate', {
        today: today,
        num: status.slice(0, 1)
      })
      // 内设部门清空审核记录
      if (status === '3') {
        param.internal_code_log = ''
      }
      // 更新状态
      const res = await this.$http('inputProjectUp', {
        id: this.verifyParams.id,
        level: this.verifyParams.status,
        check_status: '2',
        status: status,
        check_end_time: check_end_time,
        govern_check_msg: this.formData.message,
        ...param,
        log: {
          project_id: this.verifyParams.id,
          company_code: this.user.company_code,
          login_name: this.user.loginName,
          check_status: '2',
          status: this.verifyParams.status,
          target_status: status,
          message: this.formData.message,
        }
      })
      this.saveLoading = false
      if (res.errcode === 4000) {
        this.$tips.declareUp(res.errmsg)
      } else if (res.errcode === 9999) {
        this.$tips.error(res.errmsg)
      } else {
        this.$tips.success(res.errmsg)
      }

      this.$emit('cancel');
      this.$emit('init');
    },
    submit () {
      this.$confirm('是否确认上报?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.submit1()
      }).catch(() => {

      });
    },
    async submit1 () {
      /*
      先判断是否是咨询机构驳回，是提报到4
      在判断是否是电子政务部门驳回的，如果是的话直接提报到51
      然后判断部门类型
      1、普通部门提报到保障部门
      2、保障部门提报到内设部门
      3、内设部门加上审核信息，进入联评联审
      4、电子政务部门提交到内部部门联评联审
       */
      // if (!this.check()) {
      //   return true
      // }
      this.saveLoading = true
      let status
      if (this.verifyParams.status === '51') {
        status = '4'
      } else if (this.verifyParams.status === '52') {
        status = '6'
      }
      // 审核时间
      let date = new Date()
      let today = this.$util.formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      let check_end_time = await this.$http('checkDate', {
        today: today,
        num: status.slice(0, 1)
      })
      const res = await this.$http('inputProjectUp', {
        id: this.verifyParams.id,
        level: this.verifyParams.status,
        status: status,
        govern_check_msg: this.formData.message,
        check_status: '1',
        check_end_time: check_end_time,
        log: {
          project_id: this.verifyParams.id,
          company_code: this.user.company_code,
          login_name: this.user.loginName,
          check_status: '1',
          status: this.verifyParams.status,
          target_status: status,
          message: this.formData.message
        }
      })
      this.saveLoading = false
      if (res.errcode === 4000) {
        this.$tips.declareUp(res.errmsg)
      } else if (res.errcode === 9999) {
        this.$tips.error(res.errmsg)
      } else {
        this.$tips.success(res.errmsg)
      }

      this.$emit('cancel');
      this.$emit('init');
    },
    check () {
      return
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  filters: {
    f_checkStatus: f_checkStatus
  }
}
</script>
<style lang='scss' scoped>
</style>
